package com.seaboxdata.interfaceJob.swkj;

import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.RealSlData;
import com.seaboxdata.kafka.BaseStringKafkaProducer;
import com.seaboxdata.service.RealSlDataService;
import com.seaboxdata.utils.TransRealHttpsHandler;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 水务-渗流测站实时数据-入库17数据库dpswj_skhdjk，表real_sl_data
 *
 * @Author wei
 * @Date 2021/8/16 17:33
 **/
@Component
@Configuration
@Slf4j
public class RealSlDataJob {

    @Value("${swkj.url.realSlData}")
    private String url;

    @Value("${swkj.token.realSlData}")
    private String token;

    @Value("${swkj.topic.realSlData}")
    private String topic;

    @Value("${kafka.server}")
    private String kafkaServer;

    @Value("${interface.isTest}")
    private String isTest;

    @Autowired
    private BaseStringKafkaProducer baseStringKafkaProducer;

    @Autowired
    private RealSlDataService realSlDataService;

    private static final int retry = 3;

    private static int countRetry = 0;

   @Scheduled(cron = "${swkj.jobtime.realSlData}")//早晚9点各执行一次
    public void doCollect() {
        log.info("collect realSlData begin");
        //如果是测试,直接退出
        if ("true".equalsIgnoreCase(isTest)) {
            return;
        }
        try {
            List<RealSlData> realSlDataList = retryGetData(url);
            if (!CollectionUtils.isEmpty(realSlDataList)) {
                //写入数据库
                Date date = new Date();
                for (RealSlData realSlData : realSlDataList) {
                    realSlData.setCjsj(date);
                }
                realSlDataService.saveOrUpdateBatch(realSlDataList);
            }
        } catch (Exception e) {
            log.error("write realSlData error");
            e.printStackTrace();
        }
    }

    /**
     * 获取LIST
     *
     * @param url
     * @return
     * @throws Exception
     */
    public List<RealSlData> retryGetData(String url) throws Exception {
        List<RealSlData> realSlDataList = null;
        String result = "";
        try {
            result = TransRealHttpsHandler.sendPost(url, null);
            JSONObject jsonObject = JSONObject.parseObject(result);
            realSlDataList = TransRealHttpsHandler
                    .parseJSONObject(jsonObject.getJSONArray("data").toJSONString(), RealSlData.class);
            log.info("realSlDataList->{}", realSlDataList);
            if (CollectionUtils.isNotEmpty(realSlDataList)) {
                countRetry = 0;
            } else {
                if (countRetry == retry) {
                    log.error("we have try {} to getData,but still have error", countRetry);
                    return null;
                } else {
                    countRetry++;
                    Thread.sleep(1000);
                    realSlDataList = retryGetData(url);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return realSlDataList;
    }


}
